package com.eduManage.controllers;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Validator;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.eduManage.beans.Eleve;
import com.eduManage.service.EleveService;

@Controller
public class EpaceProfsControlleur {
	
	 	private EleveService eleveService;
     
	    @Autowired(required=true)
	    @Qualifier(value="eleveService")
	    public void setEleveService(EleveService eleveService){
	        this.eleveService = eleveService;
	    }
	     
	
	    @Autowired
	    @Qualifier("eleveFormValidator")
	    private Validator eleveFormValidator;
	 
	
	 	 @RequestMapping(value="/ajouterEleve", method=RequestMethod.GET)
	     public String showForm(ModelMap model){
	         Eleve eleve = new Eleve();
            model.addAttribute("eleve", eleve);
	         return "ajouterEleve";
	     }

	  

	     @RequestMapping(value="/ajouterEleve" , method=RequestMethod.POST)
	     public String processForm(@Valid Eleve eleve, BindingResult result){
	         if(result.hasErrors()){
	        	   System.out.println("In [EspaceProfsController] errors in form");
	             return "ajouterEleve";
	         }else{
	        	 
	        	 
	             System.out.println("In [EspaceProfsController] Eleve's values is : " + eleve);
	             
	             if(eleve.getId() == 0){
	                 //new person, add it
	                 this.eleveService.addEleve(eleve);
	             }else{
	                 //existing person, call update
	                 this.eleveService.updateEleve(eleve);
	             }
	             return "redirect:/listeEleves";
	         }
	     }
	     
		 @RequestMapping(value="/listeEleves", method=RequestMethod.GET)
	     public String showListeEleves(ModelMap model){
	        /* Eleve eleve = new Eleve();*/
            model.addAttribute("listeEleves", this.eleveService.listEleves());
	         return "listeEleves";
	     }
		 
		 @RequestMapping(value="/slidingMenu", method=RequestMethod.GET)
	     public String showSlidingMenu(){
	        /* Eleve eleve = new Eleve();*/
           
	         return "slidingMenu";
	     }

}
